import type { RouterScrollBehavior } from 'vue-router'
import { createRouter, createWebHashHistory } from 'vue-router'
// 路径信息
import { staticRoute } from './routes'
// 路由守卫
import registerRouteGuard from './guard/index'

const scrollBehavior: RouterScrollBehavior = () => {
  const keepPageStoreHook = withStoreOut(useKeepPageStore)()
  // 异步滚动操作
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        left: 0,
        top: keepPageStoreHook.scrollTop,
      })
    }, 0)
  })
}

const router = createRouter({
  history: createWebHashHistory(),
  routes: staticRoute,
  scrollBehavior,
})

// 注册路由守卫
registerRouteGuard(router)

export default router
